﻿using DMS.Base;
using System.Data;
using System.Data.Common;

namespace DMS.Business.BaseClass
{
    public class SerialNumberHelper
    {
        /// <summary>
        /// 获取单号
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetNewSerialNumber(string key)
        {
            string _serialNumber = "";
            DbCommand command = DBHelper.Instance.GetStoredProcCommand("sp_BuildSerailNumber");
            DBHelper.Instance.AddInParameter(command, "@key", DbType.String,key);
            DBHelper.Instance.AddInParameter(command, "@subCode", DbType.String, null);

            object serailObj = DBHelper.Instance.ExecuteScalar(command);

            if (null != serailObj) _serialNumber = serailObj.ToString();
            
            return _serialNumber;
        }

        /// <summary>
        /// 获取单号，同时支持子单号
        /// </summary>
        /// <param name="key">表单名称</param>
        /// <param name="subCode">子单号</param>
        /// <returns></returns>
        public static string GetNewSerialNumber(string key,string subCode)
        {
            string _serialNumber = "";
            DbCommand command = DBHelper.Instance.GetStoredProcCommand("sp_BuildSerailNumber");
            DBHelper.Instance.AddInParameter(command, "@key", DbType.String, key);
            DBHelper.Instance.AddInParameter(command, "@subCode", DbType.String, subCode);

            object serailObj = DBHelper.Instance.ExecuteScalar(command);

            if (null != serailObj) _serialNumber = serailObj.ToString();

            return _serialNumber;
        }

        public static void UpdateCurrentSerialNumber(string key)
        {
            string sql = string.Format("UPDATE SYS_SerialNum SET CurrentNumber=CurrentNumber+1 WHERE FormName='{0}'", key);
            DBHelper.Instance.ExecuteNonQuery(CommandType.Text, sql);
        }
    }
}
